

#![allow(dead_code)]

mod list;

struct Unit;

struct Pair(i32, f32);

struct Point {
    x: f32,
    y: f32,
}

#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

// 枚举
#[derive(Debug)]
enum WebEvent {
    PageLoad,
    PageUnload,
    KeyPress(char),
    Paste(String),
    Click { x: i64, y: i64 },
}

fn inspect(event: WebEvent) {
    match event {
        WebEvent::PageLoad => println!("page loaded"),
        WebEvent::PageUnload => println!("page unloaded"),
        WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
        WebEvent::Paste(s) => println!("pasted \"{}\".", s),
        WebEvent::Click { x, y } => {
            println!("clicked at x={}, y={}.", x, y);
        },
    }
}

// 类型别名
#[allow(dead_code)]
type Single = Unit;

enum Number {
    Zero,
    One,
    Two,
}
enum Color {
    Red = 0xff0000,
    Green = 0x00ff00,
    Blue = 0x0000ff,
}

// 常量
// const 不可更改
// static 'static生命周期，可以可变定义(unsafe)
const PI: f32 = 3.141592653589;
static WORKER: usize = 5;


///结构体，枚举， 常量
fn main() {
    let man = Person {name: "John".to_owned(), age: 18};
    println!("the man's name is: {}, age is: {}", man.name, man.age);

    let point: Point = Point{x: 0.5, y: 0.5};
    println!("point: x={}, y={}", point.x, point.y);

    let pair = Pair(1, 1.1);
    let Pair(int, decimal) = pair;
    println!("pair 0:{}  1:{}", int, decimal);


    let pressed = WebEvent::KeyPress('x');
    let pasted  = WebEvent::Paste("my text".to_owned());
    let click   = WebEvent::Click { x: 20, y: 80 };
    let load    = WebEvent::PageLoad;
    let unload  = WebEvent::PageUnload;

    inspect(pressed);
    inspect(pasted);
    inspect(click);
    inspect(load);
    inspect(unload);

    // use 引入
    // use crate::WebEvent::{PageLoad, PageUnload};
    use crate::WebEvent::*;
    let reload = PageLoad;
    println!("page reload: {:?}", reload);

    match reload {
        PageLoad => println!("reload over"),
        _ => println!(""),
    }

    // as类型转换， 枚举可以转换为整数类型
    println!("zero is {}", Number::Zero as usize);
    println!("one is {}", Number::One as i32);
    println!("roses are #{:06x}", Color::Red as i32);
    println!("violets are #{:06x}", Color::Blue as i32);
    // 枚举实现链表
    list::list_show();


    println!("PI is {}", PI);
    println!("workers is {}", WORKER);

}
